<template>
  <div class="liebiao" v-for="item in arr" :key="item.id">
    <p>{{ item.content }}</p>
    <p>{{ item.title }}</p>
    <div class="xuanzhong">
      <div class="xuanzhong bobo">
        <p>是否公开</p>
        <el-switch v-model="item.status" @change="xiugaizhuangtai(item)" />
      </div>
      <div>
        <el-button type="success" @click="bianji(item)">编辑</el-button>
        <el-button type="danger" @click="shanchu(item)">删除</el-button>
      </div>
    </div>
  </div>

  <!-- 编辑弹窗 -->
  <el-dialog v-model="dialogFormVisible" title="修改页面" width="500">
    <el-form :model="form">
      <el-form-item label="标题" :label-width="formLabelWidth">
        <el-input v-model="form.biaoti" autocomplete="off" />
      </el-form-item>
      <el-form-item label="内容" :label-width="formLabelWidth">
        <el-input v-model="form.neirong" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="queding()"> 确定修改 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import http from '@/http/http'
import { reactive, ref } from 'vue'
let arr = ref([])
//编辑弹窗

const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

const form = reactive({
  biaoti: '',
  neirong: '',
  id: 0
})

function sxlist() {
  http({
    url: '/posts',
    method: 'get'
  }).then((r) => {
    console.log(r)
    console.log('请求成功')
    r.data.reverse()
    arr.value = r.data
  })
}
sxlist()

function shanchu(item) {
  console.log(item.id)

  http({
    url: '/posts/' + item.id,
    method: 'delete'
  }).then(() => {
    console.log('删除成功')
    sxlist()
  })
}

function bianji(item) {
  console.log(item)
  dialogFormVisible.value = true
  form.biaoti = item.content
  form.neirong = item.title
  form.id = item.id
  console.log(form.id)
}
function queding() {
  dialogFormVisible.value = false
  http({
    url: '/posts/' + form.id,
    method: 'patch',
    data: {
      title: form.neirong,
      content: form.biaoti
    }
  }).then(() => {
    console.log('修改成功')
    sxlist()
  })
}

function xiugaizhuangtai(item) {
  http({
    url: '/posts/' + item.id,
    method: 'patch',
    data: {
      status: item.status
    }
  }).then(() => {
    sxlist()
  })
}
</script>

<style scoped>
.liebiao {
  width: 50%;
  border: 1px solid #cecece;
  margin: 10px;
  height: 50px;
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.xuanzhong {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.bobo {
  margin-right: 20px;
}
</style>

<!-- http({
  url: '/posts/' + item.id,
  method: 'patch',
  data: {
    title: form.neirong,
    content: form.biaoti,
    status
  }
}).then(() => {
  console.log('修改成功')
  sxlist()
}) -->
